<?php
	
	//thumbnails 添加新文章的时候，右侧设置特色图像。如果没设置，会自动读取文章中的第一张插图为缩略图，如果连文章中也没有插图，那么就读取设置好的默认图片。
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 160, 160 ,true );
	add_image_size( $name, $width, $height, $crop );
	//add post thumbnails
	if ( function_exists( 'add_theme_support' ) ) {
		add_theme_support( 'post-thumbnails' );
	}
	if ( function_exists( 'add_image_size' ) ) {
		add_image_size( 'customized-post-thumb', 100, 120 );
	}
	
	function don_the_thumbnail() 
	{
		global $post;
		
		// 判断该文章是否设置的缩略图，如果有则直接显示
		if ( has_post_thumbnail() ) 
		{
			echo '<a href="'.get_permalink().'">';
			the_post_thumbnail();
			echo '</a>';
		} 
		else //如果文章没有设置缩略图，则查找文章内是否包含图片
		{ 
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){ // 如果文章内包含有图片，就用第一张图片做为缩略图
				echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" /></a>';
			}else { // 如果文章内没有图片，则用默认的图片。
				echo '<a href="'.get_permalink().'"><img src="'.get_bloginfo('template_url').'/images/thumbnail.jpg" /></a>';
		}
    }
}

function is_type_page() {
	global $post;
	if ($post->post_type == 'page') {
		return true;
	} else {
		return false;
	}
}

function getShortTitle() 
{
	global $post;
	//echo "$post->post_title ".$post->post_title;
	//echo "$post->post_title->length ".$post->post_title->length;
	
	if(strlen($post->post_title) > 18)
	{
		echo substr($post->post_title, 0, 18) . "...";
	}
	else
	{
		echo $post->post_title;	
	}
}

//对woocommerce 支持

add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
    add_theme_support( 'woocommerce' );
}

add_action( ‘wp_enqueue_scripts’, ‘child_manage_woocommerce_styles’, 99 );
/**
* Remove WooCommerce Generator tag, styles, and scripts from the homepage.
* Tested and works with WooCommerce 2.0+
*
* @author Greg Rickaby
* @since 2.0.0
*/
function child_manage_woocommerce_styles() {
	remove_action( ‘wp_head’, array( $GLOBALS['woocommerce'], ‘generator’ ) );
	
	if ( is_front_page() || is_home() ) {
		wp_dequeue_style( ‘woocommerce_frontend_styles’ );
		wp_dequeue_style( ‘woocommerce_fancybox_styles’ );
		wp_dequeue_style( ‘woocommerce_chosen_styles’ );
		wp_dequeue_style( ‘woocommerce_prettyPhoto_css’ );
		wp_dequeue_script( ‘wc_price_slider’ );
		wp_dequeue_script( ‘wc-single-product’ );
		wp_dequeue_script( ‘wc-add-to-cart’ );
		wp_dequeue_script( ‘wc-cart-fragments’ );
		wp_dequeue_script( ‘wc-checkout’ );
		wp_dequeue_script( ‘wc-add-to-cart-variation’ );
		wp_dequeue_script( ‘wc-single-product’ );
		wp_dequeue_script( ‘wc-cart’ );
		wp_dequeue_script( ‘wc-chosen’ );
		wp_dequeue_script( ‘woocommerce’ );
		wp_dequeue_script( ‘prettyPhoto’ );
		wp_dequeue_script( ‘prettyPhoto-init’ );
		wp_dequeue_script( ‘jquery-blockui’ );
		wp_dequeue_script( ‘jquery-placeholder’ );
		wp_dequeue_script( ‘fancybox’ );
		wp_dequeue_script( ‘jqueryui’ );
	}
}

//注册钩子
//移除分类的钩子
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 60 );


?>